#include <stdio.h>

//1、顺序查找：在顺序表array[0..n-1]中查找关键字为k的记录，
//无论查找成功或失败，均通过修改seek_seq参数来返回依次查找的记录位置的序列，
// seek_seq数组已包含的元素个数等于n；查找成功时返回0，失败时返回-1。
int seq_search(int array[], int n, int k, int seek_seq[]) {
    int i;
    for (i = 0; i < n; ++i) {
        if (array[i] == k) {
            seek_seq[i] = i;
            return 0;
        }
        else {
            seek_seq[i] = i;
        }
    }
    return -1;
}

//2、二分查找：在按照递增顺序排列的有序表array[0..n-1]中进行二分查找，
// 查找成功时返回0，失败时返回-1；
// 无论查找成功或失败，均通过修改seek_seq参数来返回依次查找的记录位置的序列，
// seek_seq元素已包含的元素个数等于n。
int bin_search(int array[], int n, int k, int seek_seq[]) {
    int i, low, mid, high;
    i = 0;
    low = 0;
    high = n - 1;
    if (low > high) {
        return -1;
    }
    while (low <= high) {
        mid = (low + high) / 2;
        if (k < array[mid]) {
            high = mid - 1;
            seek_seq[i] = mid;
        }
        else if (k > array[mid]) {
            low = mid + 1;
            seek_seq[i] = mid;
        }
        else {
            seek_seq[i] = mid;
            return 0;
        }
        i++;
    }
}

void main() {
    int array[10] = { 0,1,2,3,4,5,6,7,8,9 };
    int seek_seq[10];
    int i, j;
    for (j = 0; j < 10; j++) {
        seek_seq[j] = -1;
    }
    seq_search(array, 10, 4, seek_seq);

    printf("4 ：");

    for (i = 0; i < 10; i++) {

        if (seek_seq[i] < 0)
            break;
        printf("%d\t", seek_seq[i]);
    }

    for (j = 0; j < 10; j++) {
        seek_seq[j] = -1;
    }
    bin_search(array, 10, 3, seek_seq);
    printf("\n3 ：");
    for (i = 0; i < 10; i++) {
        if (seek_seq[i] < 0)
            break;
        printf("%d\t", seek_seq[i]);
    }
}